﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_PanelChannelService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_PanelChannelEntity> _panelChannelRepository => new Repository<ec_PanelChannelEntity>();
        #endregion
        #region 获取数据
        public ec_PanelChannelEntity GetEntity(string ProjectId, string ChannelID)
        {
            try
            {
                var tableName = ProjectSugar.TableName<ec_PanelChannelEntity>(ProjectId);
                return _panelChannelRepository.AsQueryable().AS(tableName).First(x => x.ChannelID == ChannelID);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string ChannelID)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                //关联类型删除时，删除工程数据关联表记录。
                var tableName = ProjectSugar.TableName<ec_PanelChannelEntity>(ProjectId);
                var termTableName = ProjectSugar.TableName<ec_PanelStripTermEntity>(ProjectId);
                _panelChannelRepository.AsDeleteable().AS(tableName).Where(x => x.ChannelID == ChannelID).ExecuteCommand();
                SqlSugarHelper.Db.Deleteable<ec_PanelStripTermEntity>().AS(termTableName).Where(x => x.ChannelID == ChannelID).ExecuteCommand();

                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_PanelChannelEntity entity, out string NewID)
        {
            NewID = "";
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }

                if (!string.IsNullOrEmpty(keyValue))
                {

                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        //this.BaseRepository().Update(entity);
                        _panelChannelRepository.Update(entity);
                    }
                    else
                    {
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_Panel_Channel_{ProjectEntity.ProjectIndex}", "ChannelID");
                        _panelChannelRepository.AsUpdateable(entity).
                            IgnoreColumns(x => x.CreateTime).
                            IgnoreColumns(x => x.CreateUserID).
                            AS($"ec_Panel_Channel_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }

                    NewID = keyValue;
                }
                else
                {
                    entity.Create();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _panelChannelRepository.Insert(entity);
                    }
                    else
                    {
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_Panel_Channel_{ProjectEntity.ProjectIndex}");
                        _panelChannelRepository.AsInsertable(entity).AS($"ec_Panel_Channel_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                    NewID = entity.ChannelID;
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
